package com.zhuoxin.dao.admin;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

import com.zhuoxin.bean.model.News;
import com.zhuoxin.bean.model.NewsSO;
import com.zhuoxin.bean.system.Category;
import com.zhuoxin.dao.BaseDAOImpl;

public class NewsDAOImpl extends BaseDAOImpl<News, NewsSO> implements NewsDAO {

	@Override
	public String getHqlWhere(NewsSO so) {
		return null;
	}

	@Override
	public void setCriteriaParams(Criteria criteria, NewsSO so) {
		if(so.getCategory()!=null){
			criteria.createAlias("category", "c");
			
			if(so.getCategory().getId()!=null){
				criteria.add(Restrictions.eq("c.id", so.getCategory().getId()));
			}
			
		}
		
		if(StringUtils.isNotEmpty(so.getTitle())){
			criteria.add(Restrictions.ilike("title", "%"+StringUtils.trim(so.getTitle())+"%"));
		}
		
		if(so.getCode()!=null){
			criteria.add(Restrictions.eq("code", so.getCode()));
		}
		
		if(so.getSort()!=null){
			criteria.add(Restrictions.eq("sort",so.getSort()));
		}
		
	}

	@Override
	public void setQueryParams(Query query, NewsSO so) {
		
	}

	@Override
	public void switchVO2PO(News po, NewsSO so) {
		
		if(StringUtils.isNotEmpty(so.getTitle())){
			po.setTitle(StringUtils.trim(so.getTitle()));
		}
		
		if(StringUtils.isNotEmpty(so.getAuthor())){
			po.setAuthor(StringUtils.trim(so.getAuthor()));
		}
		
		if(StringUtils.isNotEmpty(so.getTtime())){
			po.setTtime(StringUtils.trim(so.getTtime()));
		}
		
		if(so.getSort()!=null){
			po.setSort(so.getSort());
		}
		
		if(so.getCategory()!=null && so.getCategory().getId()!=null){
			Category category=findById(Category.class, so.getCategory().getId());
			po.setCategory(category);
		}
		
		if(so.getCode()!=null){
			po.setCode(so.getCode());
		}
		
		if(StringUtils.isNotEmpty(so.getPath())){
			po.setPath(StringUtils.trim(so.getPath()));
		}
		
		if(StringUtils.isNotEmpty(so.getContent())){
			po.setContent(StringUtils.trim(so.getContent()));
		}
		
		if(so.getPlace()!=null){
			po.setPlace(so.getPlace());
		}
		
		if(so.getIsHot()!=null){
			po.setIsHot(so.getIsHot());
		}
	}
}